package com.study.mq;

import com.study.model.User;
import com.study.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author: chenhs
 * @date: Created in 11:09 2019/12/2
 **/
@Component
public class MessageConsumer {

    public Logger logger = LoggerFactory.getLogger(MessageConsumer.class);

    @Autowired
    private UserService userService;
    @Autowired
    private MessageProducer messageProducer;


    @RabbitListener(queues = RabbitConfig.DIRECT_ROUTING_KEY_recvqueue)
    @RabbitHandler
    public void onMessage(Message message) {
        logger.info("consumer receive message -------->: {}", message);
        User user = userService.find(new String(message.getBody()));
        try {
            messageProducer.sendMessage(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RabbitListener(queues = RabbitConfig.DIRECT_ROUTING_KEY_sendqueue)
    @RabbitHandler
    public void basicMessage(Object message) {
        System.out.println(" [x] Received '" + message + "'");
    }


}
